package lar.system.opengl;

import javax.microedition.khronos.opengles.GL11;

import lar.system.engine.MainProgram;
import lar.system.engine.TouchListener;
import lar.system.engine.UI;
import lar.system.sound.Sound;
import lar.system.R;

import android.util.Log;
import android.view.MotionEvent;

/**
 * A pre-made class that will hold
 * the classic and fully functional
 * generic game button.
 * @author Roma
 *
 */
public class GLRButton implements Drawable
{
	private GLImage 		buttonImage;
	private GLImage 		buttonImage2;
	private GLImage 		topImage=null;
	private RButtonListener event;
	private TouchListener   listener;
	private GLString 		string;
	private boolean 		pressed=false;
	private Integer 		sound;
	
	public interface RButtonListener
	{
		public void event();
	}
	
	public GLRButton(float[] glPos, float[] glDim, String str, float font, GLColor color, RButtonListener event)
	{
		sound = Integer.valueOf(R.raw.button);
		buttonImage = new GLImage(R.drawable.button, glPos, glDim);
		buttonImage2 = new GLImage(R.drawable.buttonpressed, glPos, glDim);
		this.event = event;
		string = new GLString(R.drawable.basefont, str, new float[]{glPos[0]-(str.length()-1)*font/2, glPos[1]}, font);
		string.setColor(color.asFloatArray());
		listener = new TouchListener() 
		{
			public void onTouchEvent(MotionEvent e) 
			{
				if(e.getAction() == MotionEvent.ACTION_DOWN)
				{
					pressed = true;
					MainProgram.glSurfaceView.lockUI(this);
					Sound.playSound(GLRButton.this.getSound());
				}
				else if(e.getAction() == MotionEvent.ACTION_UP)
				{
					if(!pressed) return;
					pressed = false;			
					float gl_x = 2*(e.getX() / (float)MainProgram.glSurfaceView.getWidth())-1;
					gl_x *= MainProgram.glSurfaceView.getAspectRatio();
					float gl_y = -2*(e.getY() / (float)MainProgram.glSurfaceView.getHeight())+1;
					if(MainProgram.glSurfaceView.getGUILock() == this) 
						MainProgram.glSurfaceView.lockUI(null);
					if(!CheckBounds(gl_x, gl_y)) return;
					GLRButton.this.event.event();
				}
			}
		};
		MainProgram.glSurfaceView.addTouchListener(listener, this);
	}
	
	public void setString(String str)
	{
		string.SetString(str);
	}

	public void forceEvent()
	{
		if(event != null) event.event();
	}
	
	/**
	 * Use custom images. Each one of the args may be null (omitted).
	 * @param normal_res normal picture or null
	 * @param pressed_res pressed picture or null
	 */
	public void setImages(Integer normal_res, Integer pressed_res)
	{
		if(normal_res != null) buttonImage = new GLImage(normal_res, buttonImage.getGLPos(), buttonImage.getGLDim());
		if(pressed_res != null) buttonImage2 = new GLImage(pressed_res, buttonImage2.getGLPos(), buttonImage2.getGLDim());
		UI.needRendering = true;
	}
	
	public void setSound(Integer sound_res)
	{
		sound = sound_res;
	}
	
	public void setTopImage(Integer img_res)
	{
		float[] pos = this.buttonImage.getGLPos();
		float[] dim = this.buttonImage.getGLDim();
		dim[0]*= 0.7f;
		dim[1]*= 0.7f;
		if(img_res == null) topImage = null;
		else topImage = new GLImage(img_res, pos, dim);
	}
	
	private Integer getSound()
	{
		return sound; 
	}
	
	public TouchListener getTouchListener()
	{
		return listener;
	}
	
	public void setEventListener(RButtonListener obj)
	{
		this.event = obj;
	}
	
	public void removeTouchListener()
	{
		if(listener != null) MainProgram.glSurfaceView.removeTouchListener(listener);
	}
	
	public void Draw(GL11 gl) 
	{
		if(buttonImage != null && !pressed) buttonImage.Draw(gl);
		else if(buttonImage2 != null && pressed) buttonImage2.Draw(gl);
		if(topImage != null) topImage.Draw(gl);
		string.Draw(gl);
	}

	public boolean CheckBounds(float x, float y) 
	{
		return buttonImage.CheckBounds(x, y);
	}


	
}
